#ifndef APPLICATION_MANAGER_H
#define APPLICATION_MANAGER_H

#include <QObject>
#include <QStringList>
#include <QProcess>
#include <QTimer>
#include <QJsonObject>
#include <QJsonArray>

#ifdef Q_OS_WIN
#include <windows.h>
#include <tlhelp32.h>
#include <psapi.h>
#endif

class ApplicationManager : public QObject
{
    Q_OBJECT

public:
    explicit ApplicationManager(QObject *parent = nullptr);
    ~ApplicationManager();

    // 应用程序管理
    QStringList getApplicationList();
    bool launchApplication(const QString &exePath);
    bool isApplicationRunning(const QString &exePath);
    void refreshApplicationList();

    // 进程管理
    QStringList getRunningProcesses();
    bool terminateProcess(const QString &processName);
    bool terminateProcessByPid(qint64 pid);

signals:
    void applicationListUpdated();
    void applicationLaunched(const QString &exePath, bool success);
    void applicationTerminated(const QString &processName, bool success);

private slots:
    void onRefreshTimer();

private:
    void scanInstalledApplications();
    void scanRunningProcesses();
    QString getApplicationName(const QString &exePath);
    QString getApplicationVersion(const QString &exePath);
    QString getApplicationPublisher(const QString &exePath);
    
#ifdef Q_OS_WIN
    QString getProcessExePath(DWORD processId);
    QString getProcessWindowTitle(DWORD processId);
#endif

private:
    QStringList m_installedApplications;
    QStringList m_runningProcesses;
    QTimer *m_refreshTimer;
    
    // 进程信息结构
    struct ProcessInfo {
        qint64 pid;
        QString name;
        QString exePath;
        QString windowTitle;
    };
    
    QList<ProcessInfo> m_processList;
};

#endif // APPLICATION_MANAGER_H
